package lc;

import java.util.ArrayList;
import java.util.Arrays;

public class ReverseWords {
    public String reverseWords(String s) {
        ArrayList<String> strings = new ArrayList<>(Arrays.asList(s.split(" ")));
        for (int i=strings.size()-1; i>=0; i--) {
        	String string = strings.get(i);
        	if (string.length() == 0) {
        		strings.remove(i);
        	}
        }
        StringBuilder sb = new StringBuilder();
        for (int i=strings.size()-1; i>=0; i--) {
        	sb.append(strings.get(i));
        	if (i>0) {
        		sb.append(" ");
        	}
        }
    	return sb.toString();
    }
    
    public static void main(String ...strings) {
    	ReverseWords rw = new ReverseWords();
    	System.out.println(rw.reverseWords("the sky is blue"));
    	System.out.println(rw.reverseWords(" 1"));
    }
}
